package com.manuelvs.bobbysadventure;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.SpriteBackground;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class serverorclient extends BaseGameActivity 
{
	int isserver=1;
	
	private Camera mCamera;
	
    private BitmapTextureAtlas mBitmapTextureAtlas;
    private TextureRegion Fondo;
    
    private TiledTextureRegion textureServidor;
    private TiledTextureRegion textureCliente;
    
    AnimatedSprite Servidor;
    AnimatedSprite Cliente;
    
    private Scene mMainScene;
    Boolean usesjoystick=true;
	
	@Override
	public Engine onLoadEngine() 
	{
		SharedPreferences Joystick =
			     getSharedPreferences("Joystick",Context.MODE_PRIVATE);
		usesjoystick = Joystick.getBoolean("Joystick", true);
		mCamera = new Camera(0, 0, 480, 320);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,new RatioResolutionPolicy(480, 320), mCamera));
	}
	@Override
	public void onLoadResources() 
	{
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		this.mBitmapTextureAtlas = new BitmapTextureAtlas(1024, 512, TextureOptions.BILINEAR);
		if(getText(R.string.language).toString().equals("es"))
		{
	        this.textureServidor = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "es/servidor.png", 0, 0, 1, 2);
	        this.textureCliente = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "es/cliente.png", 0, 301, 1, 2);
		}
		if(getText(R.string.language).toString().equals("pt"))
		{
	        this.textureServidor = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "pt/servidor.png", 0, 0, 1, 2);
	        this.textureCliente = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "pt/cliente.png", 0, 301, 1, 2);
		}
		if(getText(R.string.language).toString().equals("other"))
		{
	        this.textureServidor = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "server.png", 0, 0, 1, 2);
	        this.textureCliente = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "client.png", 0, 301, 1, 2);
		}
        this.Fondo = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "fondo.png", 301, 0);
        this.mEngine.getTextureManager().loadTextures(this.mBitmapTextureAtlas);
	}
	@Override
	public Scene onLoadScene() {
		
	    mMainScene = new Scene();
		SpriteBackground bg = new SpriteBackground(new Sprite(0, 0, Fondo));
		mMainScene.setBackground(bg);
		
		Servidor = new AnimatedSprite(160, 37, textureServidor)
		{
            @Override
            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY)
            {
            	if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN)
            	{
	            	Servidor.animate(new long[] {30}, new int[] {1}, 30);
	            	Servidor.setScale(1.25f);
	            	
            	}
            	if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP)
            	{
            		isserver=1;
            		Servidor.animate(new long[] {30}, new int[] {0}, 30);
            		Servidor.setScale(1f);
            		Bundle bundle = new Bundle();
            	    bundle.putInt("isserver", isserver);
            		if (!usesjoystick) 
            		{
            			final Intent Jugar = new Intent(serverorclient.this, BluetoothLogic.class);
                	    Jugar.putExtras(bundle);
                		startActivity(Jugar);
            		}
            		else
            		{
            			final Intent Jugar = new Intent(serverorclient.this, jBluetoothLogic.class);
                	    Jugar.putExtras(bundle);
                		startActivity(Jugar);
            		}
	            	
            	}
            	return true;
            }
        };
	    mMainScene.attachChild(Servidor);
	    
		Cliente = new AnimatedSprite(160, 132, textureCliente)
		{
            @Override
            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY)
            {
            	if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN)
            	{
            		Cliente.animate(new long[] {30}, new int[] {1}, 30);
            		Cliente.setScale(1.25f);
	            	
            	}
            	if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP)
            	{

            		isserver=0;
            		Cliente.animate(new long[] {30}, new int[] {0}, 30);
            		Cliente.setScale(1f);
            		Bundle bundle = new Bundle();
            	    bundle.putInt("isserver", isserver);
            		if (!usesjoystick) 
            		{
            			final Intent Jugar = new Intent(serverorclient.this, BluetoothLogic.class);
                	    Jugar.putExtras(bundle);
                		startActivity(Jugar);
            		}
            		else
            		{
            			final Intent Jugar = new Intent(serverorclient.this, jBluetoothLogic.class);
                	    Jugar.putExtras(bundle);
                		startActivity(Jugar);
            		}
	            	
            	}
            	return true;
            }
        };
	    mMainScene.attachChild(Cliente);
	    
	    mMainScene.registerTouchArea(Servidor);
	    mMainScene.registerTouchArea(Cliente);
	    mMainScene.setTouchAreaBindingEnabled(true);
		return mMainScene;
	}
	@Override
	public void onLoadComplete() {}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.more_apps:
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/developer?id=ManuelVS"));
        	startActivity(browserIntent);
            return true;
        case R.id.configuration:
        	final Intent configuration = new Intent(serverorclient.this, configuration.class);
        	startActivity(configuration);
            return true;
        }
        return false;
        
    }
	
}
